<?php
require 'db.php';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
$data = mb_convert_encoding($data, 'UTF-8', 'GBK');

// 裁头
$offset = mb_strpos($data, 'provincetr', 2000, 'GBK');
$data = mb_substr($data, $offset, null, 'GBK');

// 裁尾
$offset = mb_strpos($data, '</table>', 200,'GBK');
$data = mb_substr($data, 0, $offset,'GBK');
preg_match_all('/\d{2}|[\x7f-\xff]+/', $data, $out);
$out = $out[0];

$lists = array();
for ($i = 0; $i < count($out); $i++) {
    $lists[] = array(
        'code' => $out[$i++],
        'name' => $out[$i]
    );
}

DB::query("delete from province");
DB::insert('province', $lists);
echo 'province插入成功';
